package com.wwg.market.ui.category.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.mobileares.midp.mui.client.list.UFishListWidget;
import com.mobileares.midp.widgets.client.anchor.TextAnchor;
import com.mobileares.midp.widgets.client.list.ListItem;
import com.mobileares.midp.widgets.client.menu.PopMenu;
import com.mobileares.midp.widgets.client.pop.IMessageConfirm;
import com.mobileares.midp.widgets.client.pop.Message;
import com.mobileares.midp.widgets.client.utils.IWidgetProvider;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.wwg.market.ui.category.client.CategoryOperPageItem;
import com.wwg.market.ui.category.client.CategoryPropertiesItem;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.service.dto.CategoryDto;
import com.wwg.market.ui.service.interfaces.CategoryService;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-11-21
 * Time: 10:18:13
 * To change this template use File | Settings | File Templates.
 */
public class CategoryWidget extends Composite {
    private int width = 300;
    private int height = 300;
//    private HorizontalPanel container;
    private PopMenu popMenu;
    private UFishListWidget<CategoryDto> listWidget;
    private TextAnchor moveConfirm;
    private TextAnchor moveCancel;

    private CategoryOperPageItem page;
    private Command createChildCommand;
    private CategoryDto parentCategory;

    public void setParentCategory(CategoryDto parentCategory) {
        this.parentCategory = parentCategory;
    }

    public void setCreateChildCommand(Command createChildCommand) {
        this.createChildCommand = createChildCommand;
    }

    private IWidgetProvider provider = new IWidgetProvider<CategoryDto>() {
        public Widget getWidget(final CategoryDto ob) {
            HorizontalPanel container = new HorizontalPanel();
            Label text = new Label(ob.getName());
            container.add(text);
            container.setCellWidth(text, "100px");
            final TextAnchor oper = new TextAnchor("操作");
            oper.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent clickEvent) {
                    int x = oper.getAbsoluteLeft();
                    int y = oper.getAbsoluteTop();
                    popMenu.show(x, y + 10);
                }
            });
            final TextAnchor typePro = new TextAnchor("属性管理");
            typePro.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent clickEvent) {
                    page.getPage().switchItem(CategoryPropertiesItem.class.getName()).ddOutModel(ob);
                }
            });

            container.add(oper);
            container.add(typePro);
            return container;
        }

        public Object getUserObject(CategoryDto ob) {
            return ob;
        }
    };

    public CategoryWidget(CategoryOperPageItem page) {
        this(page, true);
    }

    public CategoryWidget(CategoryOperPageItem page, boolean createChild) {
        this.page = page;
//        container = new HorizontalPanel();
        init(createChild);
        initWidget(listWidget);
        initConfirmEvent();
    }

    private void initConfirmEvent() {
        moveConfirm = new TextAnchor("确定");
        moveConfirm.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                //todo:upList
                Utils.invoke(CategoryService.class.getName(), "moveCategoryList",
                        new Object[]{listWidget.getBMUI().getSelected().getUserObject(), getListWidgetModel()}, new LoadingAsyncCallback() {
                            @Override
                            protected void success(Object o) {
                                moveConfirm.removeFromParent();
                                moveCancel.removeFromParent();
                                listWidget.getFishModel().load((java.util.List) o);
                            }
                        });
            }
        });
        moveCancel = new TextAnchor("取消");
        moveCancel.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                moveConfirm.removeFromParent();
                moveCancel.removeFromParent();
                listWidget.getFishModel().render();
            }
        });
    }

    private java.util.List getListWidgetModel() {
        java.util.List result = new ArrayList();
        if (listWidget.getBMUI().getWidgetCount() > 0) {
            for (Iterator it = listWidget.getBMUI().getIterator(); it.hasNext();) {
                ListItem item = (ListItem) it.next();
                result.add(item.getUserObject());
            }
        }
        return result;
    }

    private void init(boolean isCreateChild) {
        popMenu = new PopMenu();
        popMenu.addMenuItem("上移", new Command() {
            public void execute() {
                int index = listWidget.getBMUI().getItemIndex(listWidget.getBMUI().getSelected());
                if (index > 0) {
                    ListItem item = listWidget.getBMUI().getSelected();
                    item.removeFromParent();
                    listWidget.getBMUI().insertItem(index - 1, item);
                    HorizontalPanel hp = (HorizontalPanel) item.getUserWidget();

                    if (hp.getWidgetIndex(moveConfirm) == -1) {
                        hp.add(moveConfirm);
                        hp.add(moveCancel);
                    }
                }
            }
        });
        popMenu.addMenuItem("下移", new Command() {
            public void execute() {
                int index = listWidget.getBMUI().getItemIndex(listWidget.getBMUI().getSelected());
                if (index < listWidget.getBMUI().getWidgetCount() - 1) {
                    ListItem item = listWidget.getBMUI().getSelected();
                    item.removeFromParent();
                    listWidget.getBMUI().insertItem(index + 1, item);
                    HorizontalPanel hp = (HorizontalPanel) item.getUserWidget();

                    if (hp.getWidgetIndex(moveConfirm) == -1) {
                        hp.add(moveConfirm);
                        hp.add(moveCancel);
                    }
                }
            }
        });
        popMenu.addMenuItem("修改", new Command() {
            public void execute() {
                page.getForm().setTitle("修改类别");
                page.getForm().setModel(listWidget.getFishModel());
                page.getForm().show(parentCategory, (CategoryDto) listWidget.getBMUI().getSelected().getUserObject());
            }
        });
        popMenu.addMenuItem("删除", new Command() {
            public void execute() {
                Message.showConfirm("温馨提示", "是否要删除当前类别?", new IMessageConfirm() {
                    public void fire(boolean confirm) {
                        if (confirm) {
                            final CategoryDto dto = (CategoryDto) listWidget.getBMUI().getSelected().getUserObject();
                            Utils.invoke(CategoryService.class.getName(), "delete", new Object[]{dto}, new LoadingAsyncCallback() {
                                @Override
                                protected void success(Object o) {
                                    listWidget.getFishModel().deleteItem(dto);
                                }
                            });
                        }
                    }
                });
            }
        });
        if (isCreateChild)
            popMenu.addMenuItem("添加子类别", new Command() {
                public void execute() {
                    //todo;
                    if (createChildCommand != null)
                        createChildCommand.execute();
                }
            });
        listWidget = getListWidget();
//        FishModelManager.getAppModel("channels").getListenerManager().addListener(Listener._LOAD, new ListenerHandler() {
//            public void execute(IFishModel model) {
//                page.initBar(1);
//            }
//        });
//        container.add(listWidget);
    }

    public void setSelectionHandler(final SelectionHandler<ListItem> handler) {
        listWidget.getBMUI().addSelectionHandler(new SelectionHandler<ListItem>() {
            public void onSelection(SelectionEvent<ListItem> listItemSelectionEvent) {
                handler.onSelection(listItemSelectionEvent);
            }
        });
    }

    private UFishListWidget<CategoryDto> getListWidget() {
        UFishListWidget<CategoryDto> list = new UFishListWidget();
        list.getBMUI().setProvider(provider);
        list.getBMUI().setWidth(width + "px");
        list.getBMUI().setHeight(height + "px");
        return list;
    }

    public void render() {
        listWidget.getFishModel().render(listWidget.getFishWidget());
    }

    public UFishListWidget<CategoryDto> getCategoryListWidget() {
        return listWidget;
    }
}
